'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Series extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.Series.belongsTo(models.Brand, {as: 'brand'})
    }
  }
  Series.init({
    brandId: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    nameZh: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: '中文系列名称必填'},
        notEmpty: {msg: '中文系列名称不能为空'},
        len: {
          args: [2, 45],
          msg: '中文系列名称长度在2-45个字符之间'
        },
        async isUnique(value) {
          const series = await Series.findOne({ where: { nameZh: value } })
          if (series) {
            throw new Error('中文系列名称已经存在。');
          }
        }
      }
    },
    nameEn: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {msg: '英文系列名称必填'},
        notEmpty: {msg: '英文系列名称不能为空'},
        len: {
          args: [2, 45],
          msg: '英文系列名称长度在2-45个字符之间'
        },
        async isUnique(value) {
          const series = await Series.findOne({ where: { nameEn: value } })
          if (series) {
            throw new Error('英文系列名称已经存在。');
          }
        }
      }
    },
    descriptionZh: DataTypes.TEXT,
    descriptionEn: DataTypes.TEXT
  }, {
    sequelize,
    modelName: 'Series',
  });
  return Series;
};